﻿using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace ConsumeSys.Common
{
    // Token: 0x02000008 RID: 8
    public sealed class RSACrypto
    {
        // Token: 0x06000046 RID: 70 RVA: 0x00002F09 File Offset: 0x00001109
        public RSACrypto(string privateKey, string publicKey = null)
        {
            if (!string.IsNullOrEmpty(privateKey))
            {
                this._privateKeyRsaProvider = this.CreateRsaProviderFromPrivateKey(privateKey);
            }
            if (!string.IsNullOrEmpty(publicKey))
            {
                this._publicKeyRsaProvider = this.CreateRsaProviderFromPublicKey(publicKey);
            }
        }

        // Token: 0x06000047 RID: 71 RVA: 0x00002F3B File Offset: 0x0000113B
        public string Decrypt(string cipherText)
        {
            if (this._privateKeyRsaProvider == null)
            {
                throw new Exception("_privateKeyRsaProvider is null");
            }
            return Encoding.UTF8.GetString(this._privateKeyRsaProvider.Decrypt(Convert.FromBase64String(cipherText), false));
        }

        // Token: 0x06000048 RID: 72 RVA: 0x00002F6C File Offset: 0x0000116C
        public string Encrypt(string text)
        {
            if (this._publicKeyRsaProvider == null)
            {
                throw new Exception("_publicKeyRsaProvider is null");
            }
            return Convert.ToBase64String(this._publicKeyRsaProvider.Encrypt(Encoding.UTF8.GetBytes(text), false));
        }

        // Token: 0x06000049 RID: 73 RVA: 0x00002FA0 File Offset: 0x000011A0
        private RSACryptoServiceProvider CreateRsaProviderFromPrivateKey(string privateKey)
        {
            byte[] buffer = Convert.FromBase64String(privateKey);
            RSACryptoServiceProvider rsacryptoServiceProvider = new RSACryptoServiceProvider();
            RSAParameters parameters = default(RSAParameters);
            using (BinaryReader binaryReader = new BinaryReader(new MemoryStream(buffer)))
            {
                ushort num = binaryReader.ReadUInt16();
                if (num == 33072)
                {
                    binaryReader.ReadByte();
                }
                else
                {
                    if (num != 33328)
                    {
                        throw new Exception("Unexpected value read binr.ReadUInt16()");
                    }
                    binaryReader.ReadInt16();
                }
                num = binaryReader.ReadUInt16();
                if (num != 258)
                {
                    throw new Exception("Unexpected version");
                }
                if (binaryReader.ReadByte() != 0)
                {
                    throw new Exception("Unexpected value read binr.ReadByte()");
                }
                parameters.Modulus = binaryReader.ReadBytes(this.GetIntegerSize(binaryReader));
                parameters.Exponent = binaryReader.ReadBytes(this.GetIntegerSize(binaryReader));
                parameters.D = binaryReader.ReadBytes(this.GetIntegerSize(binaryReader));
                parameters.P = binaryReader.ReadBytes(this.GetIntegerSize(binaryReader));
                parameters.Q = binaryReader.ReadBytes(this.GetIntegerSize(binaryReader));
                parameters.DP = binaryReader.ReadBytes(this.GetIntegerSize(binaryReader));
                parameters.DQ = binaryReader.ReadBytes(this.GetIntegerSize(binaryReader));
                parameters.InverseQ = binaryReader.ReadBytes(this.GetIntegerSize(binaryReader));
            }
            rsacryptoServiceProvider.ImportParameters(parameters);
            return rsacryptoServiceProvider;
        }

        // Token: 0x0600004A RID: 74 RVA: 0x00003100 File Offset: 0x00001300
        private int GetIntegerSize(BinaryReader binr)
        {
            byte b = binr.ReadByte();
            if (b != 2)
            {
                return 0;
            }
            b = binr.ReadByte();
            int num;
            if (b == 129)
            {
                num = (int)binr.ReadByte();
            }
            else if (b == 130)
            {
                byte b2 = binr.ReadByte();
                byte b3 = binr.ReadByte();
                byte[] array = new byte[4];
                array[0] = b3;
                array[1] = b2;
                num = BitConverter.ToInt32(array, 0);
            }
            else
            {
                num = (int)b;
            }
            while (binr.ReadByte() == 0)
            {
                num--;
            }
            binr.BaseStream.Seek(-1L, SeekOrigin.Current);
            return num;
        }

        // Token: 0x0600004B RID: 75 RVA: 0x00003188 File Offset: 0x00001388
        private RSACryptoServiceProvider CreateRsaProviderFromPublicKey(string publicKeyString)
        {
            byte[] b = new byte[]
            {
                48,
                13,
                6,
                9,
                42,
                134,
                72,
                134,
                247,
                13,
                1,
                1,
                1,
                5,
                0
            };
            byte[] a = new byte[15];
            byte[] array = Convert.FromBase64String(publicKeyString);
            int num = array.Length;
            RSACryptoServiceProvider result;
            using (MemoryStream memoryStream = new MemoryStream(array))
            {
                using (BinaryReader binaryReader = new BinaryReader(memoryStream))
                {
                    ushort num2 = binaryReader.ReadUInt16();
                    if (num2 == 33072)
                    {
                        binaryReader.ReadByte();
                    }
                    else
                    {
                        if (num2 != 33328)
                        {
                            return null;
                        }
                        binaryReader.ReadInt16();
                    }
                    a = binaryReader.ReadBytes(15);
                    if (!this.CompareBytearrays(a, b))
                    {
                        result = null;
                    }
                    else
                    {
                        num2 = binaryReader.ReadUInt16();
                        if (num2 == 33027)
                        {
                            binaryReader.ReadByte();
                        }
                        else
                        {
                            if (num2 != 33283)
                            {
                                return null;
                            }
                            binaryReader.ReadInt16();
                        }
                        if (binaryReader.ReadByte() != 0)
                        {
                            result = null;
                        }
                        else
                        {
                            num2 = binaryReader.ReadUInt16();
                            if (num2 == 33072)
                            {
                                binaryReader.ReadByte();
                            }
                            else
                            {
                                if (num2 != 33328)
                                {
                                    return null;
                                }
                                binaryReader.ReadInt16();
                            }
                            num2 = binaryReader.ReadUInt16();
                            byte b2 = 0;
                            byte b3;
                            if (num2 == 33026)
                            {
                                b3 = binaryReader.ReadByte();
                            }
                            else
                            {
                                if (num2 != 33282)
                                {
                                    return null;
                                }
                                b2 = binaryReader.ReadByte();
                                b3 = binaryReader.ReadByte();
                            }
                            byte[] array2 = new byte[4];
                            array2[0] = b3;
                            array2[1] = b2;
                            int num3 = BitConverter.ToInt32(array2, 0);
                            if (binaryReader.PeekChar() == 0)
                            {
                                binaryReader.ReadByte();
                                num3--;
                            }
                            byte[] modulus = binaryReader.ReadBytes(num3);
                            if (binaryReader.ReadByte() != 2)
                            {
                                result = null;
                            }
                            else
                            {
                                int count = (int)binaryReader.ReadByte();
                                byte[] exponent = binaryReader.ReadBytes(count);
                                RSACryptoServiceProvider rsacryptoServiceProvider = new RSACryptoServiceProvider();
                                rsacryptoServiceProvider.ImportParameters(new RSAParameters
                                {
                                    Modulus = modulus,
                                    Exponent = exponent
                                });
                                result = rsacryptoServiceProvider;
                            }
                        }
                    }
                }
            }
            return result;
        }

        // Token: 0x0600004C RID: 76 RVA: 0x000033A4 File Offset: 0x000015A4
        private bool CompareBytearrays(byte[] a, byte[] b)
        {
            if (a.Length != b.Length)
            {
                return false;
            }
            int num = 0;
            for (int i = 0; i < a.Length; i++)
            {
                if (a[i] != b[num])
                {
                    return false;
                }
                num++;
            }
            return true;
        }

        // Token: 0x04000008 RID: 8
        private RSACryptoServiceProvider _privateKeyRsaProvider;

        // Token: 0x04000009 RID: 9
        private RSACryptoServiceProvider _publicKeyRsaProvider;
    }
}
